package com.website.system.controller;

import com.website.common.controller.BaseController;
import com.website.system.model.User;
import com.website.system.service.UserService;
import com.website.system.utils.GetMess;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class RegisterController extends BaseController {
    @Resource
    private UserService userService;

    /**
     * 跳转到注册页面
     */
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register() {
            return "manage/register";
    }

    /**
     * 验证码
     */
    @ResponseBody
    @RequestMapping(value = "/sendSMS", method = RequestMethod.GET)
    public String sendSMS(@RequestParam String phone) {
        //根据获取到的手机号发送验证码
        String random=(int)((Math.random()*9+1)*100000)+"";
        GetMess.getRequest1();
        GetMess.getRequest2(phone, random);
        return random;
    }

    /**
     * 注册提交
     */
    @ResponseBody
    @RequestMapping("/userAdd")
    public String userAdd(User user,@RequestParam String birthdayForm){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birthday;
        try{
            if(birthdayForm != null && !birthdayForm.equals("")){
                birthday = simpleDateFormat.parse(birthdayForm);
                user.setBirthday(birthday);
            }
            user.setCreateTime(new Date());
            user.setState(SHOW_STATE);
            user.setLevel(MANAGEMENT_LEVEL);
            userService.insert(user);
            //发送注册成功QQ邮件
            GetMess.emile(user.getEmail());
            return SUCCESS_MSG;
        }catch (Exception e){
            return ERROR_MSG;
        }
    }
}
